package cn.iocoder.yudao.adminserver.franky.strategy;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 策略模式优惠券使用
 * @Author: franky
 * @CreateDate: 2021/4/19 2:10 下午
 * @Version: 1.0
 */
//策略定义
public interface DiscountStrategy {
    double calDiscount(Order order);
}

class NormalDiscountStrategy implements DiscountStrategy {
    @Override
    public double calDiscount(Order order) {
        System.out.println("这是普通优惠");
        return 0.1d;
    }
}

class GrouponDiscountStrategy implements DiscountStrategy {
    @Override
    public double calDiscount(Order order) {
        System.out.println("这是拼团优惠");
        return 0.2d;
    }
}

class PromotionDiscountStrategy implements DiscountStrategy {
    @Override
    public double calDiscount(Order order) {
        System.out.println("这是促销活动");
        return 0.3d;
    }
}

class Order {
    private OrderType discountType;
    private Double price;

    public OrderType getDiscountType() {
        return discountType;
    }

    public void setDiscountType(OrderType discountType) {
        this.discountType = discountType;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }
}

enum OrderType{
    NORMAL,
    GROUPON,
    PROMOTION;

}
// 策略创建
class DiscountFactory {
    public static final Map<OrderType, DiscountStrategy> strategies = new HashMap<>();
    static {
        strategies.put(OrderType.NORMAL, new NormalDiscountStrategy());
        strategies.put(OrderType.GROUPON, new GrouponDiscountStrategy());
        strategies.put(OrderType.PROMOTION, new PromotionDiscountStrategy());
    }

    public static DiscountStrategy getDiscountStrategy(OrderType orderType) {
        return strategies.get(orderType);
    }
}
//策略使用
class OrderService {
    public double discount(Order order) {
        OrderType orderType = order.getDiscountType();
        return DiscountFactory.getDiscountStrategy(orderType).calDiscount(order);
    }
}




